#-*- coding:UTF-8 -*-
'''
Created on 2013-6-5

@author: hanjiewu
'''
# r,w,a,+
#r 以读方式打开
'''
rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)
w 以写方式打开 (必要时清空)
a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+ 以读写模式打开
w+ 以读写模式打开 (参见 w )
a+ 以读写模式打开 (参见 a )
rb 以二进制读模式打开
wb 以二进制写模式打开 (参见 w )
ab 以二进制追加模式打开 (参见 a )
rb+ 以二进制读写模式打开 (参见 r+ )
wb+ 以二进制读写模式打开 (参见 w+ )
ab+ 以二进制读写模式打开 (参见 a+ )
a. Python 2.3 中新增
'''
import os
#file = open(r'F:\workspace\mypython\bigFile.txt')
#file.seek()
for tmpdir in (r'F:\workspace\mypython\tmp', r'c:\temp'):
    if os.path.isdir(tmpdir):
        break
    else:
        print 'no temp directory available'
        tempdir = ''

if tmpdir:
    os.chdir(tmpdir)
    cwd = os.getcwd()
    print '*** current temporary directory'
    print cwd

print '*** creating exmaple directory...'
os.mkdir('example');
os.chdir('example');
cwd = os.getcwd()
print '*** new working directory:'
print cwd
print '*** original directory listing:'
print os.listdir(cwd)

print '*** creating test file...'
fobj = open("test", 'w')
fobj.write("foo\n")
fobj.write("bar\n")
fobj.close()

print '*** updated directory listing:'
print os.listdir(cwd)

print '*** renaming "test" to "filetest.txt"'
os.rename('test', 'filetest.txt')
print '*** updated directory listing:'
print os.listdir(cwd)

path = os.path.join(cwd, os.listdir(cwd)[0])
print '*** full file pathname'
print path

print '*** (pathname, basename)=='
print os.path.split(path)

print '*** (filename, extension)=='
print os.path.splitext(os.path.basename(path))

print '*** displaying file contents:'
fobj = open(path)
for eachLine in fobj:
    print eachLine
fobj.close()

print '*** deleting test file'
os.remove(path)
print '*** updated directory listing:'
print os.listdir(cwd)

os.chdir(os.pardir)

print '*** deleing test directory'
os.rmdir('example')
print '*** DONE'

